<?php

if (!defined('IN_OSIC'))
{
    trigger_error('Hacking attempt', E_USER_ERROR);
}

function getConf($key = '') {
	global $conn, $smarty;
	
	$settings = '';
	$condition = '';
	
	if(!empty($key)) {
		$condition = " and key = '$key' ";
	}
	
	$sql = "select * from settings where 1".$condition;
	$rs = $conn->Execute($sql);

	while(!$rs->EOF){
		$val['id'] = $rs->Fields['id']->Value;
		$val['key'] = $rs->Fields['key']->Value;
		$val['val'] = $rs->Fields['val']->Value;
		$val['val'] = str_replace("\r\n", "<br>", $val['val']);
		$val['description'] = $rs->Fields['description']->Value;
		
		$settings[$val['key']] = $val;
		$rs->MoveNext(); 
	}
	
	$smarty->assign('conf', $settings);
}

function getLink() {
	global $conn, $smarty;

	$sql = "select * from links order by position DESC, updated DESC";
	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$result['lid'] = $rs->Fields['lid']->Value;
		$result['updated'] = $rs->Fields['updated']->Value;
		$result['title'] = $rs->Fields['title']->Value;
		$result['url'] = $rs->Fields['url']->Value;
		$result['position'] = $rs->Fields['position']->Value;
		
		$data[] = $result;
		$rs->MoveNext(); 
	}
	
	$smarty->assign('links', $data);
}

function getArticles($top = '', $sql = '') {
	global $conn, $smarty;
	
	$sql = "select $top * from article where 1 " . $sql . " order by  pos asc,rank desc, id desc";

	$rs = $conn->Execute($sql);
	$result = array();
	while(!$rs->EOF)
	{
		$data['id'] 	 = $rs->Fields['id']->Value;
		$data['cid'] 	 = $rs->Fields['cid']->Value;
		$data['title']	 = $rs->Fields['title']->Value;
		$data['title']	 = stripslashes($data['title']);
		$data['curtitle'] = cutstr($data['title'],24);
		
		$result[] = $data;
		
		$rs->MoveNext();
	}
	
	return $result;
}

function getArticle($sql = '') {
	global $conn, $smarty;
	
	$sql = "select * from article where 1 " . $sql;

	$rs = $conn->Execute($sql);
	
	$data['title']	 = $rs->Fields['title']->Value;
  	$data['content'] = $rs->Fields['content']->Value;
	$data['title']	 = stripslashes($data['title']);
	$data['content'] = stripslashes($data['content']);
	
	$smarty->assign('content', $data);
}

function getArticleList($perpage = '5', $index = false, $sql = '') {
	global $conn, $smarty;
	require_once('include/page.class.php');
	
	$start = ($_GET['page'] - 1) * $perpage;
	$pages = new page(array('total'=>getTotal('article'," and pos=0 $sql "), 'perpage'=>$perpage));
	$pages->pagebarnum = 5;

	if($index){
	  $sql="SELECT top $perpage * FROM article where pos=0 $sql order by rank desc,id desc";
	}
	else{
	  if($start > 0)
		  $sql="SELECT top $perpage * FROM article where pos =0 $sql and id not in(select top $start id from article where pos=0 $sql order by rank desc, id desc) order by rank desc,id desc";
	  else
		  $sql="SELECT top $perpage * FROM article where pos=0 $sql order by rank desc,id desc";
	}
	
	$rs = $conn->Execute($sql);
	
	$article = $result =  array();
	while(!$rs->EOF)
	{
		$article['id'] = $rs->Fields['id']->Value;
		$article['cid'] = $rs->Fields['cid']->Value;
		$article['title'] = stripslashes($rs->Fields['title']->Value);
		$article['curtitle'] = cutstr($article['title'],24);
		
		$result[] = $article;
		
		$rs->MoveNext(); 
	}
	
	$smarty->assign('page', $pages->show(70));
	
	return $result;
}

function getTotal($table = '', $where = '') {
	global $conn;

	$total = $conn->Execute("SELECT count(*) as c FROM $table where 1 ".$where);

	return $total->Fields['c']->Value;
}

function getField($table = '', $field = '*', $where = '') {
	global $conn;

	$result = $conn->Execute("SELECT $field FROM $table where 1 ".$where);

	$arr = $data = array();
	$arr = explode(',', $field);
	foreach($arr as $k=>$v)
	{
		$data[$v] = $result->Fields[$v]->Value;
	}
	return $data;
}

function getRecommend() {
	global $conn, $smarty;
	
	$sql = "select top 3 * from recommend order by pos DESC, updated DESC";
	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$result['id'] = $rs->Fields['id']->Value;
		$result['title'] = $rs->Fields['title']->Value;
		$result['url'] = $rs->Fields['url']->Value;
		$result['pic'] = $rs->Fields['pic']->Value;
		$result['curtitle'] = cutstr($result['title'],26);
		
		$data[] = $result;
		$rs->MoveNext(); 
	}
	
	$smarty->assign('recommend', $data);
}

function getTags() {
	global $conn, $smarty;
	
	$sql = "select top 12 id,destin from tour order by rank desc, id desc";
	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$result['id'] = $rs->Fields['id']->Value;
		$result['destin'] = $rs->Fields['destin']->Value;
		$result['destin'] = explode(' ', $result['destin']);
		
		if(is_array($result['destin'])) {
			foreach($result['destin'] as $key=>$val) {
				$k = $val . $result['id'];
				$data[$k] = $val;
			}
		}
		
		$rs->MoveNext(); 
	}

	$smarty->assign('tags', array_unique($data));
}

function getTour($condition = '') {
	global $conn;
	
	$condition && extract($condition);
	
	$sql="SELECT $top * FROM tour where 1 ".$where." order by rank desc,id desc";
	$rs = $conn->Execute($sql);
	
	$tour = $result =  array();
	while(!$rs->EOF || $single)
	{
		$tour['id'] 	 = $rs->Fields['id']->Value;
		$tour['title']   = stripslashes($rs->Fields['title']->Value);
		$tour['destin']  = stripslashes($rs->Fields['destin']->Value);
		$tour['destin']  = explode(' ', $tour['destin']);
		$tour['agency']	 = $rs->Fields['agency']->Value;
		$tour['pic']	 = $rs->Fields['pic']->Value;
		$tour['place']	 = $rs->Fields['place']->Value;
		$tour['goday']	 = $rs->Fields['goday']->Value;
		$tour['day']	 = $rs->Fields['day']->Value;
		$tour['descrip'] = stripslashes($rs->Fields['descrip']->Value);
		$tour['add_time'] = date('Y-m-d', $rs->Fields['add_time']->Value);
		
		$result[$tour['id']] = $tour;
		
		if($single)
			break;
		else
			$rs->MoveNext(); 
	}

	return $result;
}

function getTourList($perpage = '5') {
	global $conn, $smarty;
	require_once('include/page.class.php');
	
	$start = ($_GET['page'] - 1) * $perpage;
	$pages = new page(array('total'=>getTotal('tour'), 'perpage'=>$perpage));
	$pages->pagebarnum = 5;

	if($start > 0)
		$sql="SELECT top $perpage * FROM tour where id not in(select top $start id from tour order by rank desc, id desc) order by rank desc,id desc";
	else
		$sql="SELECT top $perpage * FROM tour order by rank desc,id desc";
	
	$rs = $conn->Execute($sql);
	
	$tour = $result =  array();
	while(!$rs->EOF)
	{
		$tour['id'] = $rs->Fields['id']->Value;
		$tour['title'] = stripslashes($rs->Fields['title']->Value);
		$tour['destin'] = stripslashes($rs->Fields['destin']->Value);
		$tour['ifshow'] = stripslashes($rs->Fields['ifshow']->Value);
		$tour['rank'] = $rs->Fields['rank']->Value;
		
		$result[] = $tour;
		
		$rs->MoveNext(); 
	}
	
	$smarty->assign('page', $pages->show(70));
	
	$smarty->assign('tours', $result);
}

function getNav($condition = '') {
	global $conn;
	
	$condition && extract($condition);
	
	$sql="SELECT $top * FROM article where 1 ".$where." order by rank desc,id desc";
	$rs = $conn->Execute($sql);
	
	$result = $article = array();
	while(!$rs->EOF)
	{
		$article['id'] = $rs->Fields['id']->Value;
		$article['title'] = stripslashes($rs->Fields['title']->Value);
		
		$result[] = $article;
		
		$rs->MoveNext(); 
	}
	
	return $result;
}

function get_cate_list($sql_add = '')
{
	global $conn;
	
	$sql="SELECT * FROM category where 1 ".$sql_add." order by sort_order desc,cid asc";
	//echo $sql;
	$rs = $conn->Execute($sql);
	
	$article = $result =  array();
	while(!$rs->EOF)
	{
		$cat['cid'] = $rs->Fields['cid']->Value;
		$cat['name'] = stripslashes($rs->Fields['name']->Value);
		$cat['parent_id'] = $rs->Fields['parent_id']->Value;
		$cat['sort_order'] = $rs->Fields['sort_order']->Value;
		$cat['code'] = $rs->Fields['code']->Value;
		
		$result[$cat['cid']] = $cat;
		
		$rs->MoveNext(); 
	}
	
	return $result;
}

function get_cate_info($id)
{
	global $conn;
	
	$sql="SELECT * FROM category where cid = ".$id."";

	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$cat['cid'] = $rs->Fields['cid']->Value;
		$cat['name'] = stripslashes($rs->Fields['name']->Value);
		$cat['parent_id'] = $rs->Fields['parent_id']->Value;
		$cat['sort_order'] = $rs->Fields['sort_order']->Value;
		$cat['code'] = $rs->Fields['code']->Value;
		
		$rs->MoveNext(); 
	}
	
	return $cat;
}

function get_cate_article_count($id)
{
	global $conn;
	
	$sql="SELECT count(*) as c FROM article where cid = ".$id;

	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$cat['c'] = $rs->Fields['c']->Value;
		
		$rs->MoveNext(); 
	}
	
	return $cat;
}

function cate_unique($name, $parent)
{
	global $conn;
	
	$sql="SELECT count(*) as c FROM category where name='$name' and parent_id=$parent";

	$rs = $conn->Execute($sql);
	
	return $rs->Fields['c']->Value ? false : true;
}

function get_cate_min_id($parent_id = '0')
{
	global $conn;
	
	$sql="SELECT min(sort_order) as mso FROM category where parent_id = $parent_id";

	$rs = $conn->Execute($sql);
	
	return $rs->Fields['mso']->Value ? ($rs->Fields['mso']->Value - 1) : 255;
}

function getGuestBookList($perpage = '5', $sql_add = '') {
	global $conn, $smarty;
	require_once('../include/page.class.php');
	$total = getTotal('guestbook', $sql_add);
	$perpage = 15;
	$start = ($_GET['page'] - 1) * $perpage;
	$page=new page(array('total'=>$total, 'perpage'=>$perpage ));
	
	if($start > 0)
		$sql="SELECT top $perpage * FROM guestbook where id not in(select top $start id from guestbook where 1 $sql_add order approved asc,id desc) ".$sql_add." order by approved asc,id desc";
	else
		$sql="SELECT top $perpage * FROM guestbook where 1 ".$sql_add." order by approved asc,id desc";
	
	$rs = $conn->Execute($sql);
	
	$guestbook = $result =  array();
	while(!$rs->EOF)
	{
		$guestbook['id'] = $rs->Fields['id']->Value;
		$guestbook['parent_id'] = $rs->Fields['parent_id']->Value;
		$guestbook['add_time'] = date('Y-m-d H:i', $rs->Fields['add_time']->Value);
		$guestbook['sender'] = $rs->Fields['sender']->Value;
		$guestbook['e_mail'] = $rs->Fields['e_mail']->Value;
		$guestbook['homepage'] = $rs->Fields['homepage']->Value;
		$guestbook['title'] = stripslashes($rs->Fields['title']->Value);
		$guestbook['message'] = stripslashes($rs->Fields['message']->Value);
		$guestbook['approved'] = $rs->Fields['approved']->Value;
		$guestbook['ip_address'] = $rs->Fields['ip_address']->Value;
		$guestbook['reply'] = getTotal('guestbook', " and parent_id = ".$guestbook['id']);
		
		$result[] = $guestbook;
		
		$rs->MoveNext(); 
	}
	
	$smarty->assign('page', $page->show(40));
	
	return $result;
}

function get_gb_info($id)
{
	global $conn, $smarty;
	
	$sql="SELECT * FROM guestbook where id = ".$id."";

	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$guestbook['id'] = $rs->Fields['id']->Value;
		$guestbook['title'] = stripslashes($rs->Fields['title']->Value);
		$guestbook['ip_address'] = $rs->Fields['ip_address']->Value;
		$guestbook['parent_id'] = $rs->Fields['parent_id']->Value;
		$guestbook['add_time'] = date('Y-m-d H:i', $rs->Fields['add_time']->Value);
		$guestbook['sender'] = $rs->Fields['sender']->Value;
		$guestbook['e_mail'] = $rs->Fields['e_mail']->Value;
		$guestbook['homepage'] = $rs->Fields['homepage']->Value;
		$guestbook['message'] = $rs->Fields['message']->Value;
		$guestbook['reply'] = get_gb_reply($guestbook['id']);
		
		$rs->MoveNext(); 
	}
	
	$smarty->assign('gb_mesg', $guestbook);
}

function get_gb_reply($id)
{
	global $conn;
	
	$sql="SELECT * FROM guestbook where parent_id = ".$id."";
	
	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$guestbook['id'] = $rs->Fields['id']->Value;
		$guestbook['e_mail'] = $rs->Fields['e_mail']->Value;
		$guestbook['sender'] = $rs->Fields['sender']->Value;
		$guestbook['add_time'] = date('Y-m-d H:i', $rs->Fields['add_time']->Value);
		$guestbook['message'] = $rs->Fields['message']->Value;
		
		$rs->MoveNext(); 
	}
	
	return $guestbook;
}

function get_imgs($top = "top 10", $condition = '')
{
	global $conn;
	
	$sql="SELECT $top * FROM image where 1 $condition order by img_sort desc";
	
	$rs = $conn->Execute($sql);
	
	$result = $imgs = array();
	
	while(!$rs->EOF)
	{
		$imgs['id'] 		= $rs->Fields['id']->Value;
		$imgs['img_text'] 	= $rs->Fields['img_text']->Value;
		$imgs['img_url'] 	= $rs->Fields['img_url']->Value;
		$imgs['img_thumb'] 	= $rs->Fields['img_thumb']->Value;
		$imgs['img_src'] 	= $rs->Fields['img_src']->Value;
		$imgs['img_sort'] 	= $rs->Fields['img_sort']->Value;
		$imgs['img_type'] 	= $rs->Fields['img_type']->Value;
		
		$result[] = $imgs;
		
		$rs->MoveNext(); 
	}
	
	return $result;
}

function get_img_info($id)
{
	global $conn;
	
	$sql="SELECT * FROM image where id = ".$id."";
	
	$rs = $conn->Execute($sql);
	
	while(!$rs->EOF)
	{
		$imgs['id'] 		= $rs->Fields['id']->Value;
		$imgs['img_text'] 	= $rs->Fields['img_text']->Value;
		$imgs['img_url'] 	= $rs->Fields['img_url']->Value;
		$imgs['img_thumb'] 	= $rs->Fields['img_thumb']->Value;
		$imgs['img_sort'] 	= $rs->Fields['img_sort']->Value;
		$imgs['img_type'] 	= $rs->Fields['img_type']->Value;
		
		$rs->MoveNext(); 
	}
	
	return $imgs;
}

?>